﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;
using System.Web.SessionState;
using Axis.Dao.Config;
using Axis.BusinessObject;
using System.Xml;
using System.Text;
using Axis.BusinessObject.Axis.BusinessObject;
using Axis.Dao.Product;
using Axis.BusinessObject.Product;
using Axis.Dao.Partner;
using Axis.Dao.Contact;


namespace Sotipha.Web.Admin.Views.Contact.Service
{
    /// <summary>
    /// Summary description for ContactService
    /// </summary>
    public class ContactService : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                case "DEACTIVE":
                case "ACTIVE":
                case "DELETEMULTI":
                    output = UpdateContract(context);
                    break;
                default:
                    output = GetList(context);
                    break;
            }
            context.Response.Write(output);
        }
        protected string UpdateContract(HttpContext context)
        {

            string inputValue = GetInputValue(context);
            /* Action */
            CContactDao coreProductGroup = new CContactDao();
            CApplicationMessage message = coreProductGroup.ActionContact(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        protected string GetList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            CContactDao coreProductGroup = new CContactDao();
            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];

            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            IList<CContact> list = coreProductGroup.GetContact(inputValue);
            int totalRecords = coreProductGroup.LastOutputValue.TotalRows;
            int totalPages = coreProductGroup.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CContact> projects, int numberOfRows, int pageIndex, int totalPages)
        {
            if (projects != null && projects.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CContact project in projects)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = project.ID;
                    row.cell = new string[7];
                    row.cell[0] = project.ID.ToString();
                    row.cell[1] = project.Name;
                    row.cell[2] = project.Phone;
                    row.cell[3] = project.Email;
                    row.cell[4] = project.Message;
                    row.cell[5] = project.LastUpdatedDateTime.ToString();
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        public string GetInputValue(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            root.AppendChild(InputValue);
            InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            InputValue.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());

            StringBuilder qsXml = new StringBuilder();
            string[] allKeys = request.Form.AllKeys;
            if (allKeys.Length == 0)
            {
                allKeys = request.QueryString.AllKeys;
            }
            foreach (string pKey in allKeys)
            {
                string key = pKey.Trim();
                string value = request[key].Trim();

                InputValue.SetAttribute(key, value);
            }
            return root.InnerXml;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}